package com.mezz.bones.framework.core.thread.config;

import com.mezz.bones.framework.core.thread.WrappedThreadPoolTaskExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class ThreadPoolConfig extends AsyncConfigurerSupport {

    @Bean(name = "threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        WrappedThreadPoolTaskExecutor executor = new WrappedThreadPoolTaskExecutor();
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() + 1); // 核心线程数
        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 2 + 1); // 最大线程数
        executor.setQueueCapacity(100000); // 队列容量
        executor.setThreadNamePrefix("AsyncThread-"); // 线程名称前缀
        executor.setWaitForTasksToCompleteOnShutdown(true); // 关闭时等待任务完成
        executor.initialize();
        return executor;
    }

}
